1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <title>MyC Compiler Sample
</title>
5 <link rel=
"stylesheet" type=
"text/css" href=
"../../../docs/rotor.css">
11 <h1>MyC Compiler Sample
</h1>
14 <h2>Sample Overview
</h2>
17 <p>The goal of the MyC compiler is to show the implementation of various
18 features of the CLI.
</p>
21 <p>The following table shows the command-line options for this sample.
</p>
23 <table border=
"1" width=
"90%">
29 <td>Generates debug information.
<tr>
31 <td>Prevents generation of debug information.
<tr>
33 <td>Generates an assembly listing (.asm).
<tr>
35 <td>Creates a DLL assembly.
38 <td>Creates an executable assembly.
40 <td><b>/outdir:path
</b>
41 <td>Creates output files in the current directory.
</tr>
45 <h3>Architecture Diagram
</h3>
48 <p align=
"center"><img border=
"1" src=
"mycarch.gif" width=
"372" height=
"733"></p>
51 <p align=
"left"> </p>
54 <h3>MyC Language Specification
</h3>
56 <p>The goal of the MYC compiler is to show common intermediate language (CIL)
57 instructions and the CLI in action.
</p>
59 <p>The compiler itself is a simple recursive descent parser with a single pass
60 code generator. It generates an assembler source file which is then used as
61 input to the IL Assembler.
</p>
62 <h4>Language Specification
</h4>
63 <p>The language is a subset of the C language with
64 simplified declarations, both external and local.
</p>
66 <p>The only supported data types are
<b>int
</b> and
<b>void
</b>. Limiting
67 these choices allows a simpler sample compiler design.
</p>
68 <h4>Data Declaration
</h4>
69 <p>Variables can be declared static or local. Implicit
71 static declarations occur outside of function declarations. Local declarations
72 can occur only at the beginnings of functions prior to statements.
</p>
86 In this example the variable
<i>x
</i> is a static declaration (by default) and the
87 variable
<i>y
</i> is a local variable in function
<b>func1()
</b>. The variable
89 <i>z
</i> is actually a static, even though it is declared within a function.
90 <h4>Flow Control
</h4>
92 <LI><b>if-else
</b> blocks
93 <LI><b>while
</b> loops
94 <LI><b>for
</b> loops
</LI>
96 <h4>Restrictions
</h4>
99 <LI>Outer declarations only
100 <LI>Static methods and function calls only
</LI>
102 <h3>Backus-Naur Definition
</h3>
105 <p>letter ::=
"A-Za-z
";
<br>
106 digit ::=
"0-
9";
<br>
108 name ::= letter { letter | digit };
<br>
109 integer ::= digit { digit };
<br>
111 ident ::= name | function_call;
<br>
112 function_call ::= name
"(
" [expr {, expr}]
")
";
<br>
114 factor ::= (ident | integer |
"(
" expr
")
" );
<br>
115 unary_factor ::= [
"+
"|
"-
"] factor;
<br>
117 term1 ::= [
"*
"|
"/
"] factor;
<br>
118 term0 ::= factor { term1 };
<br>
119 first_term ::= unary_factor term1;
<br>
121 math_expr ::= first_term { [
"+
"|
"-
"] term0 }
<br>
122 rel_expr ::= math_expr (
"==
"|
"!=
"|
"<"|
">"|
">=
"|
"<=
") math_expr;
<br>
123 not_factor ::= [
"!
"] rel_expr;
<br>
124 term_bool ::= not_factor { (
"&" |
"&&") not_factor };
<br>
125 bool_expr ::= term_bool { (
"|
" |
"^
") term_bool };
<br>
126 expr ::= bool_expr;
<br>
128 assign = ident
"=
" expr;
<br>
129 assign_stmt ::= assign
";
" ;
<br>
130 if_stmt ::=
"if
" "(
" expr
")
" stmt_block [
"else
" inner_block ];
<br>
131 while_stmt ::=
"while
" "(
" expr
")
" inner_block;
<br>
132 for_stmt ::=
"for
" "(
" assign
";
" expr
";
" assign
")
" inner_block
<br>
133 break_stmt ::=
"break
" ";
";
<br>
134 cont_stmt ::=
"continue
" ";
";
<br>
135 ret_stmt ::=
"return
" expr
";
";
<br>
145 inner_block ::=
"{
" { stmt }
"}
";
<br>
146 outer_block ::=
"{
" { inner_decl } { stmt }
"}
";
<br>
148 inner_decl ::= [ class ] type ident {
",
" ident }
";
";
<br>
150 class ::=
"extern
" |
"static
" |
"auto
";
<br>
151 type ::=
"int
" |
"void
";
<br>
153 params ::= type ident { , type ident };
<br>
154 outer_decl ::= [ class ] type ident {
",
" ident }
";
";
<br>
155 func_decl ::= [ class ] type ident
"(
" params
")
" outer_block;
<h2>Sample Source and Build Output Locations
</h2>
158 <p>The sample source is found in sscli20\samples\compilers\myc\src directory.
The source
163 <li><a href=
"src/asm.cs">asm.cs
</a></li>
164 <li><a href=
"src/emit.cs">emit.cs
</a></li>
165 <li><a href=
"src/exe.cs">exe.cs
</a></li>
166 <li><a href=
"src/iasm.cs">iasm.cs
</a></li>
167 <li><a href=
"src/io.cs">io.cs
</a></li>
168 <li><a href=
"src/myc.cs">myc.cs
</a></li>
169 <li><a href=
"src/parse.cs">parse.cs
</a></li>
170 <li><a href=
"src/tok.cs">tok.cs
</a></li>
171 <li><a href=
"src/var.cs">var.cs
</a></li>
172 <li><a href=
"src/varlist.cs">varlist.cs
</a></li>
176 <p>The build output location is %_NTTREE%\samples\compilers\myc.
177 The output file is an executable assembly named myc.exe.
</p>
180 <h2>Building the Sample
</h2>
183 <p>All samples are built from the buildall script.
</p>
186 <p>You can also build all the
187 samples by switching to the root of the sample directory, sscli20\samples, and typing
188 <code>build -c
</code>.
</p>
191 <p>You can build this specific sample by switching to the sample directory and typing
192 <code>build -c
</code>.
</p>
195 <h2>Running the Sample
</h2>
198 <p>Because the MyC language does not support referencing external assemblies,
199 use the Runtime Debugger to step through test applications built using MyC.
</p>
203 <li>Run env.bat.
</li>
204 <li>Build the SSCLI using the buildall script or batch file.
</li>
205 <li>Switch to the %_NTTREE%\samples\compilers\myc directory.
</li>
206 <li>Type the following command:
<blockquote>
209 <p>clix myc /debug
<i>inputfile
</i></p>
215 <p>where
<i>inputfile
</i> is the name of a MyC source file.
For example:
</p>
221 <p>clix myc /debug tflow.myc
</p>
226 <li>Debug the resulting assembly:
</li>
232 <p>cordbg tflow.exe
</p>
241 <p>The debug line number count is off by
1.
</p>
246 <li><i>Modern Compiler Implementation in C
</i>, by Andrew W Appel.
</li>
247 <li><i>Algorithms in C++
</i> by Robert Sedgewick.
</li>
248 <li><i>Advanced Compiler Design and Implementations
</i>, by Steven S Muchnick.
</li>
249 <li><i>Compiler Construction
</i>, by Niklaus Wirth.
</li>
250 <li><i>Principles of Compiler Design
</i>, by Alfred V. Aho and and Jeffrey D. Ullman.
</li>
257 <p><i>Copyright (c)
2006 Microsoft Corporation. All rights reserved.
</i></p>